package com.example.bili.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bili.R;
import com.example.bili.adapter.homepageAdaper;
import com.example.bili.allbean.details_avBean;
import com.example.bili.allbean.details_avBean2;
import com.example.bili.allbean.details_avBean3;
import com.example.bili.base.BaseActivity;
import com.example.bili.module.OkHttp;
import com.example.bili.view.fragment.d_abstract;
import com.example.bili.view.fragment.d_comment;
import com.facebook.drawee.view.SimpleDraweeView;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Request;

import static com.example.bili.internet.connectors.AV;

/**
 * Created by Administrator on 2017/3/12.
 */

public class DetailsActivity_av extends BaseActivity implements OkHttp.DataCallBack, View.OnClickListener {
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentArrayList;
    private SlidingTabLayout slidingTabLayout;
    private SimpleDraweeView simpleDraweeView;
    private TextView title;
    private ImageView finishi_img;
    private details_avBean3 details_bavean3;
    private details_avBean2 details_avbean2;
    private details_avBean details_avbean;

    @Override
    protected void initview() {
        finishi_img = (ImageView) findViewById(R.id.details_topleft_img);
        finishi_img.setOnClickListener(this);
        viewPager = (ViewPager) findViewById(R.id.details_vp);
        slidingTabLayout = (SlidingTabLayout) findViewById(R.id.details_label_stl);
        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.details_topbg_img);
        title = (TextView) findViewById(R.id.details_title_tv);

        fragmentArrayList = new ArrayList<>();
    }

    @Override
    public int getLayoutView() {
        return R.layout.detailsactivity;
    }

    @Override
    protected void m_oncreate() {
        getfragmentpage();
        viewPager.setAdapter(new homepageAdaper(getSupportFragmentManager(), fragmentArrayList));
        slidingTabLayout.setViewPager(viewPager, new String[]{"简介", "评论"});
        getinternet();
    }

    public void getinternet() {
        OkHttp.getAsync(AV+"&aid="+getIntent().getStringExtra("avid"),this);
    }

    @Override
    public void requestFailure(Request request, IOException e) {

    }

    @Override
    public void requestSuccess(String result) throws Exception {
        JSONObject jsonObject = new JSONObject(result);
        JSONObject data = jsonObject.optJSONObject("data");
        String copyright = data.optString("copyright");
        if (copyright.equals("3")) {
            details_bavean3 = new Gson().fromJson(result, details_avBean3.class);
            setview3(details_bavean3);
        }else if(copyright.equals("2")){
            details_avbean2 = new Gson().fromJson(result, details_avBean2.class);
            setview2(details_avbean2);
        }else {
            details_avbean = new Gson().fromJson(result, details_avBean.class);
            setview(details_avbean);
        }
        jsonObject = null;
    }

    public void setview(details_avBean details_avBean) {
        title.setText(details_avBean.getData().getTitle());
        simpleDraweeView.setImageURI(details_avBean.getData().getPic());

    }

    public void setview2(details_avBean2 details_avBean2) {
        title.setText(details_avBean2.getData().getTitle());
        simpleDraweeView.setImageURI(details_avBean2.getData().getPic());

    }

    private void setview3(details_avBean3 details_avBean3) {
        title.setText(details_avBean3.getData().getTitle());
        simpleDraweeView.setImageURI(details_avBean3.getData().getPic());

    }

    public void getfragmentpage() {
        //简介
        fragmentArrayList.add(new d_abstract());
        //评论99989
        fragmentArrayList.add(new d_comment());
    }


    @Override
    public void onClick(View view) {
        finish();
    }

}
